import json


def fetch_uart(controls):
    uart1 = []
    uart2 = []
    uart3 = []
    if controls:
        for con in controls:
            name = con.get("name")
            if name == "uart1":
                uart1.append(con)
            if name == "uart2":
                uart2.append(con)
            if name == "uart3":
                uart3.append(con)
    return uart1, uart2, uart3


def fetch_value(uart):
    baudrate_value = ""
    device_value = ""
    device_sensor = ""
    for item in uart:
        attribute = item.get("attribute")
        if attribute == "baudrate":
            baudrate_value = item.get("value")
        if attribute == "device":
            device_value = item.get("value")
            device_sensor = item.get("sensor").get("value")
    uart_value = {
        "baudrate": baudrate_value,
        "device_value": device_value,
        "device_sensor": device_sensor
    }
    return uart_value


r_control = [{
    "name": "uart1_baudrate",
    "value": "9600"
}
    ,
    {
        "name": "uart1_device",
        "value": "sensor"
    }
    ,
    {
        "name": "uart1_sensor_id",
        "value": "33"
    }
    ,
    {
        "name": "uart2_baudrate",
        "value": "9600"
    }
    ,
    {
        "name": "uart2_device",
        "value": "sensor"
    }
    ,
    {
        "name": "uart2_sensor_id",
        "value": "1"
    }
    ,
    {
        "name": "uart3_baudrate",
        "value": "115200"
    }
    ,
    {
        "name": "uart3_device",
        "value": "sensor"
    }
    ,
    {
        "name": "uart3_sensor_id"
    }
    ,
    {
        "name": "ai",
        "value": "sensor"
    }
    ,
    {
        "name": "di",
        "value": "sensor"
    }
    ,
    {
        "name": "do",
        "value": "sensor"
    }
]


def replace_c(replace_control, uart1_value, uart2_value, uart3_value):
    values = {
        "uart1_baudrate": uart1_value.get("baudrate"),
        "uart1_device": uart1_value.get("device_value"),
        "uart1_sensor_id": uart1_value.get("device_sensor"),
        "uart2_baudrate": uart2_value.get("baudrate"),
        "uart2_device": uart2_value.get("device_value"),
        "uart2_sensor_id": uart2_value.get("device_sensor"),
        "uart3_baudrate": uart3_value.get("baudrate"),
        "uart3_device": uart3_value.get("device_value"),
        "uart3_sensor_id": uart3_value.get("device_sensor")
    }
    for value_key in values:
        for item in replace_control:
            name = item.get("name")
            if name == value_key:
                item["value"] = values.get(value_key)
    return replace_control


# def change_to_e(file_path, r_c):
#     with open(file_path) as f:
#         content = json.load(f)
def change_to_e(content, r_c):
    items = content.get("data")
    controls = []
    r_item = {}
    for item in items:
        group = item.get("group")
        if group == "connect":
            controls = item.get("controls")
            r_item = item
            break
    if r_item:
        urat1, urat2, urat3 = fetch_uart(controls)
        u_v1 = fetch_value(urat1)
        u_v2 = fetch_value(urat2)
        u_v3 = fetch_value(urat3)
        new_connect = replace_c(r_c, u_v1, u_v2, u_v3)
        r_item["controls"] = new_connect
    return content


